package com.gujia.service;

import com.gujia.util.JedisAdapter;
import com.gujia.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by GuJia on 2017/7/4.
 */
@Service
public class LikeService {

    @Autowired
    private JedisAdapter jedisAdapter;

    //获取评论点赞人数
    public long getLikeCount(String entityType,int entityId){
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        return jedisAdapter.scard(likeKey);
    }

    //点赞
    public long like(int userId,String entityType,int entityId){

        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        jedisAdapter.sadd(likeKey,String.valueOf(userId));

        //将踩的删除掉

        String disLikeKey = RedisKeyUtil.getDisLikeKey(entityType,entityId);

        jedisAdapter.srem(disLikeKey,String.valueOf(userId));

        return jedisAdapter.scard(likeKey);
    }

    //点踩
    public long disLike(int userId,String entityType,int entityId){


        String disLikeKey = RedisKeyUtil.getDisLikeKey(entityType,entityId);
        jedisAdapter.sadd(disLikeKey,String.valueOf(userId));

        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        jedisAdapter.srem(likeKey,String.valueOf(userId));

        return jedisAdapter.scard(likeKey);
    }


    public int getLikeStatus(int userId,String entityType,int entityId){
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);

        if (jedisAdapter.sismember(likeKey,String.valueOf(userId))){
            return 1;
        }

        String disLikeKey = RedisKeyUtil.getDisLikeKey(entityType,entityId);

        return jedisAdapter.sismember(disLikeKey, String.valueOf(userId)) ? -1 : 0;

    }
}
